﻿using Dapper;
using OMG.Fill.Vegetable.Model.Ent;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace OMG.Fill.Vegetable.Service
{
    /// <summary>
    /// 表1蔬菜基本情况调查T_VegBasicInfo通用操作类
    /// </summary>
    public class VegBasicInfos
    {
        private IDbConnection _conn;
        public IDbConnection dbCon
        {
            get
            {
                return _conn = ConnectFactory.GetOpenConnection();
            }
        }
        /// <summary>
        /// 添加
        /// </summary>
        public virtual ResResult<int> AddInfo(VegBasicInfoEntity model) {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"INSERT INTO dbo.T_VegBasicInfo";
            strSql +="(";
            strSql +="Id,";
            strSql +="ReportId,";
            strSql +="VegArea,";
            strSql +="PerennialArea,";
            strSql +="SeasonArea,";
            strSql +="WSArea,";
            strSql +="SFArea,";
            strSql +="LiquidArea,";
            strSql +="FacilityArea)";
            strSql +=" values(";
            strSql +="@Id,";
            strSql +="@ReportId,";
            strSql +="@VegArea,";
            strSql +="@PerennialArea,";
            strSql +="@SeasonArea,";
            strSql +="@WSArea,";
            strSql +="@SFArea,";
            strSql +="@LiquidArea,";
            strSql +="@FacilityArea)";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    model.Id= CustomFun.GetGuid();
                    int result = dbCon.Execute(strSql, model);
                    if (result > 0)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "添加成功！";
                    }
                    else
                    {
                        obj.model = result;
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "添加失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("新增表1蔬菜基本情况调查T_VegBasicInfo对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        public virtual ResResult<int> UpdateInfo(VegBasicInfoEntity model)
        {
            ResResult<int> obj = new ResResult<int>();
            ReportServices reportServices = new ReportServices();
            if (model != null)
            {
                string strSql = @"UPDATE dbo.T_VegBasicInfo set ";
                strSql += "ReportId = @ReportId,";
                strSql += "VegArea = @VegArea,";
                strSql += "PerennialArea = @PerennialArea,";
                strSql += "SeasonArea = @SeasonArea,";
                strSql += "WSArea = @WSArea,";
                strSql += "SFArea = @SFArea,";
                strSql += "LiquidArea = @LiquidArea,";
                strSql += "Remark = @Remark,";
                strSql += "FacilityArea = @FacilityArea ";
                strSql += " where Id = @Id";
                try
                {
                    using (var dbCon = ConnectFactory.GetOpenConnection())
                    {
                        int result = dbCon.Execute(strSql, model);
                        int resultReport = reportServices.UpStatus(model.ReportId,"2", "").status; //更新当前报表状态为已填报
                        if (result >= 0 && resultReport == 1)
                        {
                            obj.model = result;
                            obj.status = DefaultKey.SuccessResult;
                            obj.message = "数据更新成功！";
                        }
                        else
                        {
                            obj.model = result;
                            obj.status = DefaultKey.InfoResult;
                            obj.message = "更新失败！数量为:" + result;
                        }
                        dbCon.Dispose();//数据库连接释放
                    }
                }
                catch (Exception ex)
                {
                    obj.status = DefaultKey.ErrorResult;
                    obj.message = ex.ToString().Trim();
                    LogHelper.ErrorLog("更新表1蔬菜基本情况调查T_VegBasicInfo对象信息失败", ex);
                }
            }
            return obj;
        }
        /// <summary>
        /// 删除
        /// </summary>
        public virtual ResResult<int> DeleteInfo(string id)
        {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"delete from dbo.T_VegBasicInfo where Id = @Id";
            int result;
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    result = dbCon.Execute(strSql, new { Id = id });
                    if (result > 0)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "删除成功！";
                    }
                    else
                    {
                        obj.model = result;
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "删除失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("删除表1蔬菜基本情况调查T_VegBasicInfo对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        public virtual ResResult<int> DeleteBatch(string[] ids)
        {
            ResResult<int> obj = new ResResult<int>();
            string strIds = string.Join(",", ids.Select(x => x.ToString()).ToArray());
            string strSql = @"delete from dbo.T_VegBasicInfo where Id in @ids";
            var dynamicSqlParam = new DynamicParameters();
            dynamicSqlParam.Add("ids", ids.ToArray(), null, null, null);
            int result;
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    result = dbCon.Execute(strSql, dynamicSqlParam);
                    if (result > 0)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "删除成功！";
                    }
                    else
                    {
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "删除失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                LogHelper.ErrorLog("获取数据字典表批量删除表1蔬菜基本情况调查T_VegBasicInfo表失败", ex);
                obj.message = ex.ToString().Trim();
                obj.status = DefaultKey.ErrorResult;
                return obj;
            }
        }
        /// <summary>
        /// 获取指定对象
        /// </summary>
        public virtual ResResult<VegBasicInfoEntity> GetModel(string id) {
            ResResult<VegBasicInfoEntity> obj = new ResResult<VegBasicInfoEntity>();
            string strSql = @"select * from dbo.T_VegBasicInfo where Id = @Id";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    obj.model = dbCon.Query<VegBasicInfoEntity>(strSql, new { Id = id }).FirstOrDefault();
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("获取表1蔬菜基本情况调查T_VegBasicInfo对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 获取全部列表
        /// </summary>
        /// <returns></returns>
        public virtual ResResult<List<VegBasicInfoEntity>> GetList()
        {
            ResResult<List<VegBasicInfoEntity>> obj = new ResResult<List<VegBasicInfoEntity>>();
            string strSql = @"select * from dbo.T_VegBasicInfo";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    obj.model = dbCon.Query<VegBasicInfoEntity>(strSql).ToList();
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("更新表1蔬菜基本情况调查T_VegBasicInfo对象信息失败", ex);
                return obj;
            }
        }
    }
}